﻿#region usings
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using InsuranceAdjuster;
using InsuranceAdjuster.Helpers;
using InsuranceAdjuster.OccasionallyConnected;
using InsuranceAdjuster.OccasionallyConnected.Messages;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using Papa.Common;
#endregion
namespace InsuranceAdjuster.ViewModels {
    public class SyncStatusViewModel : ViewModelBase {
        private ObservableCollection<Message> _FilteredCollection = new ObservableCollection<Message>();
        private Boolean _ShowCompletedMessages = false;

        public RelayCommand OfflineModeCommand { get; set; }
        public RelayCommand SyncNowCommand { get; set; }

        public SyncStatusViewModel(IPageConductor pageConductor) {
            this.PageConductor = PageConductor;
            RegisterCommands();

            EntityManager.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(EntityManager_PropertyChanged);
            ProcessFiltering();
        }
        
        public ObservableCollection<Message> Messages {
            get {
                return EntityManager.Messages;
            }
        }
        public ObservableCollection<Message> FilteredMessages {
            get {
                return _FilteredCollection;
            }
        }
        public bool ShowCompletedMessages {
            get { return _ShowCompletedMessages; }
            set {
                if (_ShowCompletedMessages == value)
                    return;

                _ShowCompletedMessages = value;
                RaisePropertyChanged("ShowCompletedMessages");
                ProcessFiltering();
            }
        }
        
        public void EntityManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
            switch (e.PropertyName) {
                case "Messages":
                    RaisePropertyChanged("Messages");
                    ProcessFiltering();
                    break;
            }
        }
        protected override void RegisterCommands() {
            OfflineModeCommand = new RelayCommand(EntityManager.ToggleOnOfflineMode);
            SyncNowCommand = new RelayCommand(() => EntityManager.CacheMonitor.SyncNow(), () => EntityManager.IsOnline);
        }
        private void ProcessFiltering() {
            _FilteredCollection = new ObservableCollection<Message>();
            if (ShowCompletedMessages)
                _FilteredCollection = EntityManager.Messages
                                                   .OrderBy(x => x.ExecutionStartedAt)
                                                   .ToObservableCollection();
            else
                _FilteredCollection = EntityManager.Messages
                                                   .Where(x => x.IsActive)
                                                   .OrderBy(x => x.ExecutionStartedAt)
                                                   .ToObservableCollection();

            RaisePropertyChanged("FilteredMessages");
        }
    }
}
